@echo off
:: ----------------------------------------------------------------------------
:: Initial values; these can be overwritten in user.bat
:: ----------------------------------------------------------------------------
set vs90path=
set postBuildCall=test.bat
set buildType=Release

:: ----------------------------------------------------------------------------
:: user.bat Data
:: ----------------------------------------------------------------------------
:: If there is a user.bat data file in this folder, execute it to set some parameters
if exist "user.bat" (
	call "user.bat"
)


:: ----------------------------------------------------------------------------
:: Include the VS Tools environment
:: ----------------------------------------------------------------------------
:: We can skip this if it was set already
where vcbuild > NUL 2> NUL
if "%ERRORLEVEL%" == "0" goto endInclude
:: Find the Visual Studio Tools folder
for %%p in (
	"%VS90COMNTOOLS%",
	"C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools",
	"C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\",
) do (
	:: Check path - note that there is no "break" command in batch files
	if exist %%p (
		set vs90path=%%p
	)
)
:: Replace those double quotes; we needed them becuase of the ( ) in the paths, and we couldn't escape VS90COMNTOOLS
set vs90path=%vs90path:"=%

if "%vs90path%" == "" (
	:: Couldn't find it at all; setting up a user.bat file for them if they do not have one
	if not exist "user.bat" (
		echo set vs90path=C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools > user.bat
	)
	echo Could not find the Visual Studio 9.0 Tools directory. Please edit your user.bat file in this directory with the correct location.
	exit /B
)

:: Now try to call their batch file
if exist "%vs90path%\vsvars32.bat" (
	call "%vs90path%\vsvars32.bat"
) else (
	echo Could not load the Visual Studio 9.0 Tools environment. If you specified the directory in your user.bat file in this directory, please check to make sure the path is correct.
	exit /B
)
:endInclude


:: ----------------------------------------------------------------------------
:: Get the name of the solution in the current directory
:: ----------------------------------------------------------------------------
:: This is apparently the only way to get an output into a variable? Clear first, loop through output and append
set slnFile=
for /F "tokens=1 delims=" %%a in ('dir /b *.sln') do set slnFile=%%a
:: Test if it worked
if not exist "%slnFile%" (
	echo Could not find a Visual Studio solution file ^(.sln^) in this directory
	exit /B
)

:: -----------------------------------------------------------------------------
:: Check if this is Release or Debug
:: -----------------------------------------------------------------------------
if not "%1" == "" if exist ".\%buildType%\" (
	set buildType=%1
	:: If we were using test+debug, use copyf instead
	if "%1" == "debug" if "%postBuildCall%" == "test.bat" (
		set postBuildCall=copyf.bat
	)
	
)

:: ----------------------------------------------------------------------------
:: Build the solution
:: ----------------------------------------------------------------------------
:: First, kill the module loader if we have it open
taskkill /IM "AIModuleLoader.exe" > NUL 2> NUL
:: Build using VC Tools environment path setup
vcbuild %slnFile% "%buildType%|Win32"
:: If build was successful, call our post-build script
if %ERRORLEVEL% == 0 if exist "%postBuildCall%" (call "%postBuildCall%" %1)

